<template>
    <div class="order-tab">
        <nut-tabs v-model="tab" size="normal" title-scroll title-gutter="10" background="#ffffff" name="tab"
            @click="changeTab" v-if="user.uuid">
            <nut-tab-pane v-for="item in tabList" :key="item.key" :pane-key="item.key" :title="item.title">
                <div class="order-list" v-for="ele in orderList">
                    <block v-if="ele != null">
                        <div class="order-item" v-for="(itemVal, index) in ele"
                            @click="jumpTo('/packageA/pages/myOrderList/details?id=' + itemVal.ID)">
                            <div class="order-no flex-a flex-b">
                                <div>订单编号：{{ itemVal.order_No }}</div>
                                <div>{{ itemVal.status == '0' ? '待付款' : itemVal.status == '1' ? '待接单' : itemVal.status
                                    ==
                                    '3' ? '待服务'
                                    : itemVal.status == '4' ? '进行中' : itemVal.status == '5' ? '已完成' :
                                        itemVal.status == '6' ? '待评价' : itemVal.status == '-1' ? '待退款'
                                            : itemVal.status == '-2' ? '已退款' : itemVal.status == '-3' ? '已取消' : itemVal.status
                                                ==
                                                '-4' ? '已申请退款' : '' }}
                                </div>
                            </div>
                            <div class="order-head flex-a flex-b">
                                <div class="left">
                                    <img :src="itemVal.pageSetting.remark" alt="">
                                </div>
                                <div class="right">
                                    <div class="title flex-a flex-b">
                                        <div>{{ itemVal.pageSetting.project_Name }}</div>
                                        <div>￥{{ itemVal.service_Amount }}</div>
                                    </div>
                                    <div class="spec flex-a flex-b">
                                        <div>{{ itemVal.pageSettingSec.project_Name }}</div>
                                        <div>{{ 'x' + itemVal.count }}</div>
                                    </div>
                                </div>
                            </div>
                            <div class="time flex-a">
                                <div>服务时长：{{ itemVal.count * 60 }}分钟</div>
                                <div>预约时间：{{ itemVal.service_Time }}</div>
                            </div>
                            <div class="order-foot flex-a">
                                <nut-button type="primary" color="#F6F6F6" size="small" shape="square" class="radius"
                                    style="color: #0F1234;" v-if="itemVal.status == '0'"
                                    @click.stop="resetOrder(itemVal)">取消订单</nut-button>
                                <nut-button type="primary" color="#F6F6F6" size="small" v-if="itemVal.status == '1'"
                                    style="color: #0F1234;" @click.stop="cancelOrder(itemVal)">取消订单</nut-button>
                                <nut-button plain color="#DEDEDE" size="small" v-if="itemVal.status == '3'"
                                    @click.stop="applyBack(itemVal)">申请退款</nut-button>
                                <nut-button color="#F0A93F" shape="square" class="radius" size="small"
                                    v-if="itemVal.status == '0'" @click.stop="toPay(itemVal)">立即支付</nut-button>
                            </div>
                        </div>
                    </block>
                </div>
                <div class="empty" v-if="orderList.length == 0">
                    <div><img src="https://cxj.szzkba.cn/upload/i/2024/09/25/105042.png" alt=""></div>
                    <div>暂无订单</div>
                    <div>请先预约服务</div>
                </div>
            </nut-tab-pane>
        </nut-tabs>
        <div v-else class="no-login">
            <img src="https://cxj.szzkba.cn/upload/i/2024/08/19/100429.png" alt="">
            <span>您还没有登录哦!</span>
            <nut-button color="#21C38F" @click="jumpTo('/packageA/pages/mypage/index')">去登录</nut-button>
        </div>
        <nut-dialog title="退款申请" v-model:visible="visible" @cancel="onCancel" @ok="onOk">
            <nut-form label-position="top" star-position="right">
                <nut-form-item label="退款理由：" required label-position="top" star-position="left">
                    <nut-textarea style="border: 1rpx solid #ccc;border-radius: 10rpx;padding: 10rpx;"
                        placeholder="请输入退款理由" v-model="remark" :rows="3" type="text" />
                </nut-form-item>
            </nut-form>
        </nut-dialog>
        <nut-dialog title="请选择取消理由" text-align="left" v-model:visible="remarkShow" @cancel="qxOrder" @ok="qrOrder"
            class="quOrder">
            <nut-radio-group v-model="type">
                <nut-radio-group v-model="type" text-position="left">
                    <nut-radio label="1">不想要了</nut-radio>
                    <nut-radio label="2">下错单了</nut-radio>
                    <nut-radio label="3">临时有事不方便上门</nut-radio>
                </nut-radio-group>
            </nut-radio-group>
        </nut-dialog>
    </div>
</template>

<script lang="ts">
import Taro, { useRouter } from '@tarojs/taro';
import { useReachBottom } from '@tarojs/taro';
import { Left, MoreX } from '@nutui/icons-vue-taro';
import { reactive, toRefs, onMounted } from 'vue';
import { getOrderList, pay, changeOrder } from '../../../../api/index'
export default {
    components: {
        Left,
        MoreX
    },
    setup() {
        const data = reactive({
            visible: false,
            remarkShow: false,
            type: '0',
            remark: '',
            imgUrl:
                'https://cxj.szzkba.cn/upload/i/2024/06/04/182133.jpeg',
            waybillToken: '',
            tab: 'null' as any,
            orderList: [] as any[],
            tabList: [
                {
                    title: '全部',
                    key: 'null',
                },
                {
                    title: '待付款',
                    key: 0,
                },
                {
                    title: '待接单',
                    key: 1,
                },
                {
                    title: '待服务',
                    key: 3,
                },
                {
                    title: '进行中',
                    key: 4,
                },
                {
                    title: '已完成',
                    key: 5
                },
                {
                    title: '已取消',
                    key: -3
                },
            ],
            page: 1,
            text: 'DPK364116621912',
            showBottom: false,
            refOrderNo: '',
            tkNum: '',
            token: '',
            user: {},
            order: {},
            order_No: '',
            status: '',
        });

        onMounted(() => {
            const router = useRouter();
            if (router.params.tab) {
                data.tab = router.params.tab
            }
            Taro.getStorage({
                key: 'user',
                success: function (res) {
                    data.user = res.data ? res.data : {}
                    getOrder()
                }
            })
        })
        const changeTab = (title) => {
            data.tab = title.paneKey
            data.page = 1
            data.orderList = []
            getOrder()
        }
        const getOrder = () => {
            getOrderList({ user_ID: data.user.uuid, page: data.page, pageSize: 10, status: data.tab }).then(res => {
                if (res.code == 0) {
                    let list = res.data.list, arr = [] as any
                    list.forEach(item => {
                        if (item != null) {
                            item.forEach(ele => {
                                ele.CreatedAt = reTime(ele.CreatedAt)
                            })
                        } else {

                        }
                    });
                    list.forEach(item => {
                        if (item != null) {
                            arr.push(item)
                        }
                    })
                    data.orderList = [...data.orderList, ...arr]
                    // console.log(data.orderList)
                }
            })
        }
        const resetOrder = (item) => {
            data.remarkShow = true
            data.order_No = item.order_No
            data.status = item.status
        }
        const cancelOrder = (item) => {
            data.remarkShow = true
            data.order_No = item.order_No
            data.status = item.status
        }
        const addZero = (n) => {
            return n < 10 ? '0' + n : n
        }
        const reTime = (t) => {
            let time = new Date(t)
            let y = time.getFullYear()
            let m = addZero(time.getMonth() + 1)
            let d = addZero(time.getDate())
            let h = addZero(time.getHours())
            let f = addZero(time.getMinutes())
            let s = addZero(time.getSeconds())
            return y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s
        }
        const toPay = (item) => {
            pay({
                order_No: item.order_No,
                openId: data.user.openId,
                title: '支付调用',
                remark: '',
            }).then(res => {
                if (res.code == 0) {
                    Taro.requestPayment({
                        timeStamp: res.data.timeStamp,
                        nonceStr: res.data.nonceStr,
                        package: res.data.package,
                        signType: res.data.signType,
                        paySign: res.data.paySign,
                        success: function (res) {
                            getOrder(data.tab)
                        },
                    })
                }
            })
        }
        const jumpTo = (path) => {
            Taro.navigateTo({ url: path });
        }
        const applyBack = (item) => {
            data.visible = true
            data.order = item
        }
        useReachBottom(() => {
            //console.log('onReachBottom')
            data.page++
            getOrder()
        })
        const onCancel = () => {
            data.visible = false
        }
        const onOk = () => {
            changeOrder({ order_No: data.order.order_No, status: -4, remark: data.remark }).then(res => {
                if (res.code == 0) {
                    jumpTo('/packageA/pages/findPage/index?tab=' + data.tab)
                }
            })
            data.visible = false
        }
        const qxOrder = () => {
            data.remarkShow = false
        }
        const qrOrder = () => {
            changeOrder({ order_No: data.order_No, status: data.status == '0' ? -3 : data.status == '1' ? -1 : '', remark: data.status == '1' ? data.type : '' }).then(res => {
                if (res.code == 0) {
                    // getOrder()
                    jumpTo('/packageA/pages/findPage/index?tab=' + data.tab)
                }
            })
        }
        return {
            ...toRefs(data), changeTab, jumpTo, getOrder, toPay, reTime, addZero, resetOrder, cancelOrder, applyBack, onCancel, onOk, qxOrder, qrOrder
        }
    }
};
</script>
<style lang="scss">
.order-tab {
    .nut-tabs__titles-item__line {
        background: #F0A93F !important;
        width: 30rpx !important;
    }

    .no-login {
        text-align: center;
        background: #F5F5F5;

        img {
            width: 150rpx;
            height: 150rpx;
            margin: 45% auto 10rpx;
        }

        span {
            font-size: 28rpx;
            margin-bottom: 50rpx;
        }

        .nut-button {
            width: 250rpx;
            margin-bottom: 61%;
        }
    }

    .nut-dialog__footer-ok {
        background: #21C38F !important;
    }

    .nut-dialog__footer-cancel {
        border-color: #DEDEDE !important;
        color: #DEDEDE !important;
    }

    .nut-tab-pane {
        padding: 0;
    }

    .empty {
        text-align: center;
        height: 100vh;

        img {
            width: 300rpx;
            height: 300rpx;
            margin-top: 250rpx;
        }

        div:nth-child(2) {
            font-size: 30rpx;
            font-weight: bold;
            color: #242745;
            margin: 100rpx 0 20rpx;
        }

        div:last-child {
            color: #788290;
            font-size: 24rpx;
        }
    }

    .order-list:first-child {
        padding: 40rpx 15rpx 20rpx 15rpx;
    }

    .order-list {
        padding: 20rpx 15rpx 20rpx 15rpx;
        background: #F8F8F8;
        box-sizing: border-box;

        .order-item {
            background: #fff;
            border-radius: 10rpx;
            box-sizing: border-box;
            padding: 20rpx;
            margin-top: -20rpx;

            .order-no {
                margin-bottom: 20rpx;
                border-bottom: 1rpx solid #F5F5F5;
                padding-bottom: 20rpx;

                div:first-child {
                    font-size: 24rpx;
                    color: #242645;
                }

                div:last-child {
                    color: #F0340B;
                    font-weight: bold;
                    font-size: 24rpx;
                }
            }

            .order-head {
                padding-bottom: 20rpx;
            }

            .left {
                width: 15%;

                img {
                    width: 80rpx;
                    height: 80rpx;
                    border-radius: 10rpx;
                    float: left;
                }
            }

            .right {
                width: 84%;
                position: relative;

                .title {
                    margin-bottom: 10rpx;
                    color: #191B3C;

                    div:first-child {
                        font-size: 28rpx;
                        font-weight: bold;
                    }

                    div:last-child {
                        font-size: 32rpx;
                        font-weight: bold;
                        color: #070A2D;
                    }
                }

                .spec {
                    color: #9393A1;
                    font-size: 28rpx;
                }
            }

            .time {
                color: #9899A5;
                font-size: 26rpx;
                margin: 10rpx 0;

                div:first-child {
                    margin-right: 40rpx;
                }
            }

            .order-foot {
                padding-top: 20rpx;
                justify-content: flex-end;

                .nut-button {
                    margin-left: 10rpx;
                }
            }
        }
    }
}

.tknum {
    .nut-popup {
        padding-top: 80rpx;
    }

    .btn {
        text-align: center;

        .nut-button {
            width: 60%;
            margin: 20rpx auto;
        }
    }
}
</style>